package com.sinodata.bsm.cicp.collector.common;

import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import javax.naming.Context;
import javax.naming.NamingException;
import javax.naming.ldap.InitialLdapContext;

import com.sinodata.bsm.cicp.collector.Collector;
import com.sinodata.bsm.cicp.exception.CollectorException;
import com.sinodata.bsm.common.constants.DataTypeConstants;
import com.sinodata.bsm.common.vo.ResPropertyValue;

/**
 * 
 * <p>
 * Description: 
 * </p>
 *
 * @author liulibo
 * @version 1.0

 * <p>
 * History: 
 *
 * Date                     Author         Version     Description
 * ---------------------------------------------------------------------------------
 * 2012-11-12 15:33:31      liulibo        1.0         To create
 * </p>
 *
 * @since 
 * @see
 */
public class LDAPConnection extends Collector {

    //    private static Logger logger = Logger.getLogger(LDAPConnection.class.getName());
    private static final String INITIAL_CONTEXT_FACTORY = "com.sun.jndi.ldap.LdapCtxFactory";
    private static final String SECURITY_AUTHENTICATION_SIMPLE = "simple";

    @Override
    public List<ResPropertyValue> collect(String ip) throws CollectorException {
        List<ResPropertyValue> values = new ArrayList<ResPropertyValue>();
        String[] propMetas = getPropMeta();
        ResPropertyValue propertyValueX = new ResPropertyValue();
        ResPropertyValue.PropValue[] propValues = new ResPropertyValue.PropValue[propMetas.length];
        String ldap_ip = this.getParamMap().get("LDAP Address");//LDAP地址
        String ldap_port = this.getParamMap().get("Port");//端口
        String ldap_user = this.getParamMap().get("Username");//用户名
        String ldap_password = this.getParamMap().get("Password");//密码
        String ldapURL = "LDAP://" + ldap_ip + ":" + ldap_port;
        Properties env = new Properties();
        env.put(Context.INITIAL_CONTEXT_FACTORY, INITIAL_CONTEXT_FACTORY);
        env.put(Context.SECURITY_AUTHENTICATION, SECURITY_AUTHENTICATION_SIMPLE);
        env.put(Context.SECURITY_PRINCIPAL, ldap_user);
        env.put(Context.SECURITY_CREDENTIALS, ldap_password);
        // connect to my domain controller
        env.put(Context.PROVIDER_URL, ldapURL);
        List<String[]> result = new ArrayList<String[]>();
        try {
            new InitialLdapContext(env, null);
            result.add(new String[] { "YES" });
            //propValues[0] = ResPropertyValue.createPropValue("YES", propMetas[0]);
            propValues[0] = ResPropertyValue.createPropValue(DataTypeConstants.BOOLEAN_VALUE_TRUE, propMetas[0]);
        } catch (NamingException e) {
            result.add(new String[] { "NO" });
            // propValues[0] = ResPropertyValue.createPropValue("NO", propMetas[0]);
            propValues[0] = ResPropertyValue.createPropValue(DataTypeConstants.BOOLEAN_VALUE_FALSE, propMetas[0]);
        }
        propertyValueX.setValues(propValues);
        values.add(propertyValueX);
        return values;
    }

    @Override
    public String[] getParamMeta() {
        //        return new String[] { "LDAP地址", "端口", "用户名", "密码" };
        return new String[] { "LDAP Address", "Port", "Username", "Password" };
    }

    @Override
    public String[] getPropMeta() {
        //        return new String[] { "是否成功" };
        return new String[] { "isConn" };
    }

}
